package com.socialrss.viewlisteners;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import com.socialrss.R;
import com.socialrss.accounts.FollowedPerson;
import com.socialrss.constants.SocialNetworkNames;
import com.socialrss.utils.SocialRssModel;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;


public class ConnectionsOfSelectedSocialNetwork extends BaseActivity {
	ArrayList<FollowedPerson> mylist;
	ListView mainListView;
	private LinkedHashMap<String, Boolean> selectedHolders;
	private CustomAdapter listAdapter;
	private Button commitButton;
	private String socialNetwork;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.add_more_connections);
		socialNetwork = getIntent().getExtras().getString("socialnetwork");
		selectedHolders = new LinkedHashMap<String, Boolean>();
		//configure list of connections
		if(socialNetwork.equals(SocialNetworkNames.INSTAGRAM)){
			setListWithInstagramConnections();
		}
		else if(socialNetwork.equals(SocialNetworkNames.FOURSQUARE)){
			setListWithFoursquareConnections();
		}
		else if(socialNetwork.equals(SocialNetworkNames.TWITTER)){
			setListWithTwitterConnections();
		}
		mainListView = (ListView) findViewById( R.id.connectionlist );  
	    
	    // Create CustomAdapter for mainListView  
	    listAdapter = new CustomAdapter(this, R.layout.connlist,  mylist);
	    mainListView.setAdapter( listAdapter );
	    addListenerToFinishButton();
	}
	public void addListenerToFinishButton(){
		commitButton = (Button) findViewById(R.id.addConnCommitButton);
		commitButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				AlertDialog.Builder builder1 = new AlertDialog.Builder(ConnectionsOfSelectedSocialNetwork.this);
				builder1.setMessage("All non-selected connections will be removed from database. Do you want to continue?")
					.setPositiveButton("OK", new DialogInterface.OnClickListener() {		//ask user if he is sure about this action
						@Override
						public void onClick(DialogInterface dialog, int which) {
							String connid;
							for (int i = 0; i < mylist.size(); i++) {
								connid = mylist.get(i).getUserid();
								if(!selectedHolders.containsKey(connid)){
									SocialRssModel.Instance(null).deleteConnection(mylist.get(i).getId(), socialNetwork);
								}
								
							}
							/*
							 * End of setting
							 */
							
							
							dialog.dismiss();
							Intent i = new Intent(ConnectionsOfSelectedSocialNetwork.this, ManageConnections.class);
							i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
							startActivity(i);
						}
					})
					.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							Intent i = new Intent(ConnectionsOfSelectedSocialNetwork.this, ManageConnections.class);
							i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
							startActivity(i);
						}
					});
				AlertDialog mydial = builder1.create();
				mydial.show();
			}
		});
	}
	private void setListWithFoursquareConnections(){
		mylist = SocialRssModel.Instance(null).getConnectionsOfSocialNetworkFromDB(SocialNetworkNames.SocialNetKeys.f_sq);
		for (int i = 0; i < mylist.size(); i++) {
			selectedHolders.put(mylist.get(i).getUserid(), true);
		}
	}
	private void setListWithInstagramConnections(){
		mylist = SocialRssModel.Instance(null).getConnectionsOfSocialNetworkFromDB(SocialNetworkNames.SocialNetKeys.ins);
		for (int i = 0; i < mylist.size(); i++) {
			selectedHolders.put(mylist.get(i).getUserid(), true);
		}
	}
	private void setListWithTwitterConnections(){
		mylist = SocialRssModel.Instance(null).getConnectionsOfSocialNetworkFromDB(SocialNetworkNames.SocialNetKeys.tw);
		for (int i = 0; i < mylist.size(); i++) {
			selectedHolders.put(mylist.get(i).getUserid(), true);
		}
	}
	private class CustomAdapter extends ArrayAdapter<FollowedPerson> {
		 
		  private ArrayList<FollowedPerson>  connectionList;
		 
		  public CustomAdapter(Context context, int textViewResourceId, ArrayList<FollowedPerson>  connList) {
			  super(context, textViewResourceId, connList);
			  this.connectionList = connList;
		  }
//		  public void updateList(ArrayList<FollowedPerson>  connList){
//			  connectionList = connList;
//		  }
		  private class ViewHolder {
			  TextView name;
			  TextView acctype;
			  CheckBox sel;
		  }
		  
		  
		  
		  @Override
		  public View getView(int position, View convertView, ViewGroup parent) {

			  ViewHolder holder = null;
			  Log.v("ConvertView", String.valueOf(position));
		 
			  if (convertView == null) {
				  LayoutInflater vi = (LayoutInflater)(((Activity)this.getContext()).getSystemService(Context.LAYOUT_INFLATER_SERVICE));
				  convertView = vi.inflate(R.layout.connlist, null);
			 
				  holder = new ViewHolder();
				  holder.name = (TextView) convertView.findViewById(R.id.connname);
				  holder.acctype = (TextView) convertView.findViewById(R.id.conntype);
				  holder.sel = (CheckBox) convertView.findViewById(R.id.ingroupitemlistcheckbox);
				  convertView.setTag(holder);
			  } 
			  else {
				  holder = (ViewHolder) convertView.getTag();
			  }
		 
			  final FollowedPerson conn = connectionList.get(position);
			  holder.name.setText(conn.getName());
			  holder.acctype.setText(conn.getConntype().toString());
			  holder.sel.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
						//conn.setTurnedOn(arg1);
						selectedHolders.put(conn.getUserid(), arg1);
					}
			  	});
			  Boolean inGroup = ConnectionsOfSelectedSocialNetwork.this.selectedHolders.get(conn.getUserid());
			  if(inGroup != null)
				  holder.sel.setChecked(inGroup.booleanValue());
			  else
				  holder.sel.setChecked(false);
			  holder.sel.setTag(conn);
			  return convertView;
		 
		  }
	}
	
}
